﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Carbon.Web.Forms
{
    [Serializable]
    public class FileReference
    {
        private Object m_Id = default(String);
        public Object Id
        {
            get
            {
                return m_Id;
            }
        }

        private Guid m_Key = default(Guid);
        public Guid Key
        {
            get
            {
                return m_Key;
            }
        }

        private String m_Name = default(String);
        public String Name
        {
            get
            {
                return m_Name;
            }
            set
            {
                m_Name = value;
            }
        }

        [NonSerialized]
        private byte[] m_Data = default(byte[]);
        [NonSerialized]
        private bool m_Data_calculated = false;
        public byte[] Data
        {
            get
            {
                if (!m_Data_calculated)
                {
                    m_Data = FormsContext.Current.PersistanceService.LoadBinaryData(this.Id, this.Key);
                    m_Data_calculated = true;
                }

                return m_Data;
            }
        } 

        public FileReference(Object id, Guid key, string name)
            : base()
        {
            if (key == null) throw new ArgumentNullException("key");
            if (name == null) throw new ArgumentNullException("name");

            this.m_Id = id;
            this.m_Key = key;
            this.m_Name = name;
        }
    }
}
